Imports System.Web
Imports System.Text.RegularExpressions
Public Class IWeb_Form
    Dim IWeb As New IWebservice.WebService

#Region "Properties"
    Public ReadOnly Property WebserviceURL() As String
        Get
            Return Me.txtWebsite.Text & "/DesktopModules/IWeb/webservice.asmx"
        End Get
    End Property
    Public ReadOnly Property Password() As String
        Get
            Dim strPassword As String = ""
            If Me.chkEncrypted.Checked Then
                Dim objIWebEncryption As New IWebEncryption
                strPassword = objIWebEncryption.Encryption(Me.txtPassword.Text, Me.txtKey.Text)
                strPassword = HttpUtility.UrlEncode(strPassword)
            Else
                strPassword = Me.txtPassword.Text
            End If
            Return strPassword
        End Get
    End Property
#End Region

    Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
        Dim strResponse As String = ""
        IWeb.Url = WebserviceURL
        IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials()
        Try
            strResponse = IWeb.EncryptData(TextBox1.Text, Me.txtKey.Text)
            Me.TextBox2.Text = HttpUtility.UrlDecode(strResponse)
        Catch ex As Exception
            TextBox2.Text = ex.Message
        End Try
    End Sub

    Private Sub btnUnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnEncrypt.Click
        Dim strResponse As String = ""
        IWeb.Url = WebserviceURL
        IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials()
        Try
            strResponse = IWeb.UnEncryptData(HttpUtility.UrlEncode(TextBox1.Text), Me.txtKey.Text)
            Me.TextBox2.Text = HttpUtility.UrlDecode(strResponse)
        Catch ex As Exception
            TextBox2.Text = ex.Message
        End Try
    End Sub

    Private Sub btnGetVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetVersion.Click
        Dim IWebVersion_Info As IWebVersion_Info
        Try
            IWebVersion_Info = IWebVersion.GetVersion()
            Me.lblGetVersion.Text = "Version: " & IWebVersion_Info.VersionName & " (" & IWebVersion_Info.VersionNumber.ToString() & ")"
        Catch ex As Exception
            Me.lblGetVersion.Text = ex.Message
        End Try
    End Sub

    Private Sub btnPortals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPortals.Click
        Dim i As Integer = 0
        Dim arrPortals As Array
        Dim colPortals As String = ""
        Dim strPortalNumber As String
        Dim strPortalName As String
        Dim strPortalEntry As String
        Try
            IWeb.Url = WebserviceURL
            IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials()

            arrPortals = IWeb.PortalsOnServer().Split(",")
            For i = 0 To arrPortals.Length - 1 Step 2
                strPortalNumber = arrPortals(i).ToString()
                strPortalName = arrPortals(i + 1).ToString()
                strPortalEntry = strPortalNumber & " [" & strPortalName & "]"
                colPortals = colPortals & strPortalEntry & vbCrLf
            Next
            lblPortals.Text = colPortals.ToString
        Catch ex As Exception
            Me.lblGetVersion.Text = ex.Message
        End Try
    End Sub

    Private Sub btnCheckVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckVersion.Click
        If IWebVersion.CheckVersion(10100) Then
            MessageBox.Show("The IWeb version on the server is correct")
        Else
            MessageBox.Show("The IWeb version on the server is too low")
        End If
    End Sub

    Private Sub btnGetUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetUser.Click
        Dim objUserInfo As IWebservice.UserInfo

        Try
            IWeb.Url = WebserviceURL
            IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials()
            objUserInfo = IWeb.GetUser(txtUser.Text)

            lblShowDisplayName.Text = objUserInfo.DisplayName
            lblShowFirstName.Text = objUserInfo.FirstName
            lblShowLastName.Text = objUserInfo.LastName
            lblShowEmail.Text = objUserInfo.Email
            lblShowPassword.Text = objUserInfo.Membership.Password

        Catch ex As Exception
            Me.lblShowDisplayName.Text = ex.Message
        End Try

    End Sub

    Private Sub btnCreateUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateUser.Click

        Try
            IWeb.Url = WebserviceURL
            IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials()

            lblCreateStatus.Text = IWeb.CreateUser(txtSetUserName.Text, txtFirstName.Text, txtLastName.Text, txtDisplayName.Text, txtEmail.Text, txtSetPassword.Text)

        Catch ex As Exception
            lblCreateStatus.Text = ex.Message
        End Try

    End Sub
End Class
